﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WoWConnecter.Entities;

namespace WoWConnecter.Chat
{
    /// <summary>
    /// Represents a channel ingame
    /// </summary>
    public class Channel : IChattable
    {
        /// <summary>
        /// Name of the channel
        /// </summary>
        public string Name { get; private set; }

        /// <summary>
        /// Id of the channel
        /// </summary>
        public uint Id { get; private set; }

        /// <summary>
        /// Channel flags
        /// </summary>
        public ChatChannelFlags Flags { get; private set; }
        
        /// <summary>
        /// If we are currently in this channel
        /// </summary>
        public bool Joined { get; private set; }
        
        /// <summary>
        /// Our character
        /// </summary>
        public UserCharacter Me { get; private set; }

        /// <summary>
        /// List of characters in the channel
        /// </summary>
        public Dictionary<EntityId, Character> Characters { get; private set; }

        /// <summary>
        /// An event for when messages are received
        /// </summary>
        public event Action<ChatMessage> MessageReceived;

        //We sometimes will have to wait for character information before processing the message
        private Dictionary<EntityId, ChatMessage> mPendingMessages;

        public Channel(UserCharacter me, string name, byte flags, uint channelId)
        {
            Me = me;
            Name = name;
            Flags = (ChatChannelFlags)flags;
            Id = channelId;
            Characters = new Dictionary<EntityId, Character>();
            mPendingMessages = new Dictionary<EntityId, ChatMessage>();
        }

        /// <summary>
        /// Handles incoming messages
        /// </summary>
        /// <param name="message">Incoming Message</param>
        public virtual void IncomingMessage(ChatMessage message)
        {
            if (MessageReceived != null)
                MessageReceived(message);
        }

        #region IChattable Members

        public void Say(string message, params object[] args)
        {
            Me.SendChannelMessage(string.Format(message, args), this, Me.DefaultLanguage);
        }

        #endregion

        public override string ToString()
        {
            return Name;
        }
    }
}
